package com.doyens.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class DoyensCorsConfiguration {

    @Bean
    public CorsFilter corsFilter(){
        //初始化一个cors配置对象
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.addAllowedOrigin("http://manage.doyens.com");//允许访问我的域名
        configuration.addAllowedOrigin("http://www.doyens.com");//允许访问我的域名
        //是否允许携带cookie
        configuration.setAllowCredentials(true);
        //允许的请求方式  get post  put  delete
        configuration.addAllowedMethod("*");
        //配置允许携带的头信息
        configuration.addAllowedHeader("*");
        //初始化cors配置源对象
        UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource();
        configurationSource.registerCorsConfiguration("/**",configuration);
        return new CorsFilter(configurationSource);
    }
}
